package com.kamistoat.meimeimall.common.dubboInterface;

import com.kamistoat.meimeimall.common.To.wareTo.LockOrderWareTo;
import com.kamistoat.meimeimall.common.utils.R;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

import java.util.List;

/**
 * ware模块 Dubbo 定义
 */
public interface WareDubboInterface {
    /**
     * 查询商品列表是否有库存
     *
     * @param skuIdList skuId列表
     * @return R对象，内含 "hasStockMapTo" - HasStockMapTo
     */
    R hasStock(List<Long> skuIdList);

    /**
     * 创建订单时要锁定库存.是一个分布式事务分支
     *
     * @param lockOrderWareTo LockOrderWareTo 类
     * @return R对象
     */
    @TwoPhaseBusinessAction(name = "orderLockStock", commitMethod = "orderLockStockCommit", rollbackMethod = "orderLockStockRollback")
    R orderLockStock(BusinessActionContext businessActionContext, @BusinessActionContextParameter(paramName = "lockOrderWareTo") LockOrderWareTo lockOrderWareTo);

    /**
     * 锁定库存事务的提交
     *
     * @param businessActionContext 事务上下文
     * @return 提交是否成功
     */
    boolean orderLockStockCommit(BusinessActionContext businessActionContext);

    /**
     * 锁定库存事务的回滚
     *
     * @param businessActionContext 事务上下文
     * @return 回滚是否成功
     */
    boolean orderLockStockRollback(BusinessActionContext businessActionContext);

    /**
     * 支付成功时，从锁定的库存值中扣除，真正扣除库存
     *
     * @param orderSn 订单sn
     * @return R对象
     */
    @TwoPhaseBusinessAction(name = "handleSuccessPayWare", commitMethod = "handleSuccessPayWareCommit", rollbackMethod = "handleSuccessPayWareRollback")
    R handleSuccessPayWare(BusinessActionContext businessActionContext, @BusinessActionContextParameter(paramName = "orderSn") String orderSn);

    boolean handleSuccessPayWareCommit(BusinessActionContext businessActionContext);

    boolean handleSuccessPayWareRollback(BusinessActionContext businessActionContext);

    /**
     * 下单时，以任务引擎的思路，先做任务的落库，只要任务落库成功，就视为下单阶段扣除库存成功。
     * 收到RPC请求，仅仅根据LockOrderWareTo制作订单-库存总工单，以及每件商品的单商品库存操作。
     * 不需要做分布式事务!!!! 只需要本地事务保证插入的原子性即可。
     */
    R insertOrderWareOperation(LockOrderWareTo lockOrderWareTo);
}
